package com.smartstate.dataManagement.security;


import com.smartstate.dataManagement.entity.LoginUser;
import com.smartstate.dataManagement.util.LoginParameterUtil;
import com.smartstate.dataManagement.util.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
@Slf4j
public class UserInfoInterceptor implements HandlerInterceptor {

    /**
     * 进入Controller层之前拦截请求，默认是拦截所有请求
     * @return 是否拦截当前请求，true表示拦截当前请求，false表示不拦截当前请求
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        log.info("go into preHandle method ... ");
        LoginUser loginUser = UserUtils.getLoginUser();
        if(loginUser!=null){
            LoginParameterUtil.setUserSession(loginUser.getId(), loginUser.getEnterpriseId());
        }
        return true;
    }

    /**
     * 处理完请求后但还未渲染试图之前进行的操作
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        log.info("go into postHandle ... ");
    }

    /**
     * 视图渲染后但还未返回到客户端时的操作
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        log.info("go into afterCompletion ... ");
    }
}

